﻿using Application.AppSrevices;
using Application.Dto;
using Application.IAppSrevices;
using AutoMapper;
using Domain.Entitys;
using Domain.Repositorys;
using EFCore;
using EFCore.Repositorys;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;

namespace UI.Controllers
{
    public class EMRController : Controller
    {
        private readonly IMapper mapper;
        private readonly ICFFKService cFFKService;
        private readonly ICFTKRepository cFTKRepository;
        private readonly ICFSKRepositoty cFSKRepositoty;
        private readonly ICMKService cMKService;
        private readonly ICMService cMService;
        private readonly IEMRService eMRService;

        public EMRController(IMapper mapper, ICFFKService cFFKService,ICFTKRepository cFTKRepository,ICFSKRepositoty cFSKRepositoty, ICMKService cMKService,ICMService cMService,IEMRService eMRService)
        {
            this.mapper = mapper;
            this.cFFKService = cFFKService;
            this.cFTKRepository = cFTKRepository;
            this.cFSKRepositoty = cFSKRepositoty;
            this.cMKService = cMKService;
            this.cMService = cMService;
            this.eMRService = eMRService;
        }
        public IActionResult Index()
        {
            string userName = HttpContext.Session.GetString("UTrueName");
            ViewBag.UserTrueName = userName;
            return View();
        }

        public IActionResult IndexShow()
        {
            string userName = HttpContext.Session.GetString("UTrueName");
            ViewBag.UserTrueName = userName;
            return View();
        }

        public IActionResult IndexUpdate(int id)
        {
            string userName = HttpContext.Session.GetString("UTrueName");
            ViewBag.UserTrueName = userName;
            TempData["id"] = id;
            return View();
        }

        public IActionResult Indexshengqing()
        {
            string userName = HttpContext.Session.GetString("UTrueName");
            ViewBag.UserTrueName = userName;
            return View();
        }
        /// <summary>
        /// 查询一级
        /// </summary>
        /// <returns></returns>
        public async Task<IActionResult> CFFKSelectOption()
        {
            return Json(await cFFKService.CFFKSelectOption());
        }

        /// <summary>
        /// 根据一级id查二级
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<List<CFSK>> CFSKSelectid(string id)
        {
           
            List<CFSK> cFSK = await cFSKRepositoty.CFSKSelectid(id);
            
            return await cFSKRepositoty.CFSKSelectid(id);
            
        }

        /// <summary>
        /// 根据一级二级id查三级
        /// </summary>
        /// <param name="sid"></param>
        /// <param name="fid"></param>
        /// <returns></returns>
        public async Task<List<CFTK>> CFTKSelectCFSKCFFKId(string sid, string fid)
        {
            List<CFTK> cftk = await cFTKRepository.CFTKSelectCFSKCFFKId(sid, fid);
            return await cFTKRepository.CFTKSelectCFSKCFFKId(sid, fid);
        }

        /// <summary>
        /// 查询职位分类下拉框
        /// </summary>
        /// <returns></returns>
        public async Task<IActionResult> CMKSelectMax()
        {
            return Json(await cMKService.CMKSelectMax());
        }

        public async Task<IActionResult> CMSelectId(string id)
        {
            return Json(await cMService.CMSelectId(id));
        }
        /// <summary>
        /// 添加EMR
        /// </summary>
        /// <param name="eMR"></param>
        /// <returns></returns>
        public async Task<IActionResult> EMRAddAsync777(EMRAddDto eMRAddDto)
        {
            int num = await eMRService.EMRAddAsync(eMRAddDto);
            return Json(num);
        }

        /// <summary>
        /// 删除EMR
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<int> EMRDeleteAsync777(int id)
        {
            return await eMRService.EMRDeleteAsync(id);
        }

        /// <summary>
        /// 查询总条数
        /// </summary>
        /// <returns></returns>
        public IActionResult EMRSelectCount()
        {
            int num = eMRService.EMRSelectCount();
            return Json(num);
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public async Task<IActionResult> EMRSelectFenye(int pageIndex, int pageSize)
        {
            List<EMR> eMRs = await eMRService.EMRSelectFenye(pageIndex, pageSize);
            return Json(eMRs);
        }

        /// <summary>
        /// 根据id查询需要修改的数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<IActionResult> EMRSelectId()
        {
            int id = (int)TempData["id"];
            EMR eMR = await eMRService.EMRSelectId(id);
            return Json(eMR);
        }

        /// <summary>
        /// 根据name查询数据
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public async Task<IActionResult> CMSelectName(string name)
        {
            CM cm = await cMService.CMSelectName(name);
            return Json(cm);
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="id"></param>
        /// <param name="emr"></param>
        /// <returns></returns>
        public async Task<int> EMRUpdateAsync111(EMRUpdateDto eMRUpdateDto)
        {
            return await eMRService.EMRUpdateAsync(eMRUpdateDto);
        }
    }
}
